package org.world.api.sheep.command;

import org.mc.ddd.api.BaseApiController;
import org.mc.ddd.infrastructure.response.vo.EntityCreatedVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.world.api.sheep.command.request.CreateSheepDto;
import org.world.applicaiton.sheep.command.SheepApplicationService;
import org.world.domain.aggregate.sheep.SheepAggregate;
import org.world.domain.aggregate.sheep.event.SheepCreatedEvent;

import javax.annotation.Resource;

@RestController
public class SheepCommandController extends BaseApiController {
    @Resource
    private SheepApplicationService sheepApplicationService;

    @Resource
    private SheepAggregate sheepAggregate;

    @PostMapping("/god/create/sheep")
    public EntityCreatedVo create(@RequestBody @Validated CreateSheepDto dto) {
        SheepCreatedEvent event = sheepAggregate.create(dto.transform());

        this.send(event);

        return new EntityCreatedVo(event.getSheepId());
    }

    @PostMapping("sheep/{pregnantSheepId}/give-birth")
    public void giveBirth(@PathVariable Long pregnantSheepId) {
        sheepApplicationService.giveBirth(pregnantSheepId);
    }
}